package com.atguigu.eduservice.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: HttpUtils @Description: TODO @Author NeySun @CreateTime 2022-05-31 17:43 @Version 1.0
 */
@Slf4j
public class HttpUtils {

  public static void main(String[] args) throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put("appId", "ZYUNI666");
    map.put("sign", "D9FF71C54C10298693C3A7E3279034D8");
    map.put("reqTime", "20220531175726");
    map.put(
        "param",
        "{\t\"messageType\": \"applyStatus\",\t\"applyNo\": \"DKSQ20210521000013\",\t\"orderNo\": \"0779571653319340063\",\t\"mobile\": \"15202369907\",\t\"status\": \"01\",\t\"checkOption\": \"很好\",\t\"checkTime\": \"2021-08-08 23:11:11\",\t\"prdInfo\": {\t\t\"applyNo\": \"DKSQ20210521000013\",\t\t\"orderNo\": \"0779571653319340063\",\t\t\"term\": \"12\",\t\t\"loanAmount\": \"120000.00\",\t\t\"firstPay\": \"7000\",\t\t\"lastPay\": \"113000\",\t\t\"firstPayRate\": \"12\"\t}}");
    String url =
        "https://dev-app.dtc.changan.com.cn/api/interfaces/financial/financialLoan/syncPush";
    postParam(map, url);
  }

  // url为需要请求的地址
  private static void postParam(Map<String, String> params, String url) throws Exception {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    // 将原来写入postman中的form-data数据放入Map中
    for (Map.Entry<String, String> entry : params.entrySet()) {
      method.addParameter(entry.getKey(), entry.getValue());
    }
    try {
      client.executeMethod(method);
      byte[] response1 = method.getResponseBody();
      // 获取请求后的响应体
      String result1 = new String(response1, "UTF-8");
      log.info("收到的返回信息为：{}", result1);
    } catch (IOException ex) {
      log.info("推送请求出错{}" + ex.getMessage());
    } finally {
      // 释放连接
      method.releaseConnection();
    }
  }
}
